package com.yanqu.road.server.cmd.monopolymarket;

import com.yanqu.road.entity.monopolymarket.resgrab.MonopolyMarketReportData;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.monopolymarket.MonopolyMarketModule;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/11 16:32
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_GET_REPORT, desc = "查看 战报")
public class MonopolyMarketGetReportCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketGetReportReqMsg req = MonopolyMarketProto.MonopolyMarketGetReportReqMsg.parseFrom(packet.getBytes());
        if(!MonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        MonopolyMarketModule marketModule = player.getModule(MonopolyMarketModule.class);
        MonopolyMarketProto.MonopolyMarketGetReportRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketGetReportRespMsg.newBuilder();
        switch (req.getType()){
            case 1:{
                List<MonopolyMarketReportData> reportDataList = player.getModule(MonopolyMarketModule.class).officeReportList;
                synchronized (reportDataList){
                    for(MonopolyMarketReportData item : reportDataList){
                        builder.addOfficeReportList(MonopolyMarketMgr.builderMonopolyMarketOfficeReportEntity(item));
                    }
                }
                marketModule.baseData.setEnterOfficeReportLastTime(DateHelper.getCurrentSecond());
            }
            break;
            case 2:{
                List<MonopolyMarketReportData> reportDataList = player.getModule(MonopolyMarketModule.class).resGrabReportList;
                synchronized (reportDataList){
                    for(MonopolyMarketReportData item : reportDataList){
                        builder.addOfficeReportList(MonopolyMarketMgr.builderMonopolyMarketOfficeReportEntity(item));
                    }
                }
                marketModule.baseData.setEnterResGrabReportLastTime(DateHelper.getCurrentSecond());
            }
            break;
            default:{
                ret = GameErrorCode.E_MONOPOLY_MARKET_OFFICE_TYPE_ERROR;
                return;
            }
        }
        marketModule.synRoleData();
        builder.setRet(0);
        player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_GET_REPORT, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            MonopolyMarketProto.MonopolyMarketGetReportRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketGetReportRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_GET_REPORT, builder);
        }
    }

}
